package com.holly.unit.bpmn.activiti.ext;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.holly.unit.auth.api.context.LoginContext;
import com.holly.unit.auth.api.pojo.login.LoginUser;
import com.holly.unit.bpmn.activiti.entity.ActEntrance;
import com.holly.unit.bpmn.activiti.mapper.ActEntranceMapper;
import com.holly.unit.bpmn.activiti.pojo.ActEntranceRequest;
import com.holly.unit.bpmn.activiti.pojo.dto.ActEntranceDTO;
import com.holly.unit.db.api.factory.PageFactory;
import com.holly.unit.db.api.factory.PageResultFactory;
import com.holly.unit.db.api.pojo.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description: 入口管理
 * @Author: holly
 * @Date:   2022-03-21
 * @Version: V1.0
 */
@Service
public class ActEntranceServiceImpl extends ServiceImpl<ActEntranceMapper, ActEntrance> {

    @Autowired
    private ActEntranceMapper entranceMapper;

    @Autowired
    private ActivitiZNodeService zNodeService;

    public void add(ActEntranceRequest actEntranceRequest) {
        ActEntrance actEntrance = new ActEntrance();
        BeanUtil.copyProperties(actEntranceRequest, actEntrance);
        this.save(actEntrance);
    }

    public void del(ActEntranceRequest actEntranceRequest) {
        this.removeById(actEntranceRequest.getId());
    }

    public void edit(ActEntranceRequest actEntranceRequest) {
        ActEntrance actEntrance = new ActEntrance();
        BeanUtil.copyProperties(actEntranceRequest, actEntrance);
        this.updateById(actEntrance);
    }

    public ActEntranceDTO detail(ActEntranceRequest actEntranceRequest) {
        return entranceMapper.getById(actEntranceRequest);
    }

    public List<ActEntranceDTO> findList(ActEntranceRequest actEntranceRequest) {
        List<ActEntranceDTO> entranceList = entranceMapper.getEntranceList(actEntranceRequest);
        // 获取当前登录的用户
        LoginUser loginUser = LoginContext.me().getLoginUser();
        // 根据用户名获取角色
        List<String> roleByUserId = zNodeService.queryRoleByAccount(loginUser.getAccount());
        entranceList = entranceList.stream().filter(p->{
            String roles = p.getRoles();
            if (StrUtil.isBlank(roles)) {
                return true; //未设置授权的所有人都能用
            }else {
                String[] split = roles.split(",");
                for (String role : split) {
                    if (roleByUserId.contains(role)){
                        return true;
                    }
                }
            }
            return false;
        }).collect(Collectors.toList());
        return entranceList;
    }

    public PageResult<ActEntranceDTO> findPage(ActEntranceRequest actEntranceRequest) {
        Page<ActEntranceDTO> page = entranceMapper.selectEntranceList(PageFactory.defaultPage(),actEntranceRequest);
        return PageResultFactory.createPageResult(page);
    }

    public void updateCategory(String id, String categoryId) {
        try {
            this.baseMapper.updateCategory(id, categoryId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateStatus(String id, Integer status) {
        try {
            this.baseMapper.updateStatus(id, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
